package com.platform.web.controller.module;

import com.platform.common.annotation.Log;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.core.text.Convert;
import com.platform.common.enums.BusinessType;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.framework.util.ShiroUtils;
import com.platform.module.domain.BaseConcentratorType;
import com.platform.module.domain.BaseFactory;
import com.platform.module.service.IBaseConcentratorTypeService;
import com.platform.module.service.IBaseFactoryService;
import com.platform.system.service.ISysDictDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 集中器型号数据Controller
 *
 * @author wangqi
 * @date 2020-07-13
 */
@Controller
@RequestMapping("/module/concentrator")
public class BaseConcentratorTypeController extends BaseController
{
    private String prefix = "module/concentrator/type";

    @Autowired
    private IBaseConcentratorTypeService baseConcentratorTypeService;

    @Autowired
    private IBaseFactoryService baseFactoryService;

    @RequiresPermissions("module:concentrator:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询集中器型号数据列表
     */
    @RequiresPermissions("module:concentrator:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BaseConcentratorType baseConcentratorType)
    {
        startPage();
        List<BaseConcentratorType> list = baseConcentratorTypeService.selectBaseConcentratorTypeList(baseConcentratorType);
        return getDataTable(list);
    }


    /**
     * 导出集中器型号数据列表
     */
    @RequiresPermissions("module:type:export")
    @Log(title = "集中器型号数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BaseConcentratorType baseConcentratorType)
    {
        List<BaseConcentratorType> list = baseConcentratorTypeService.selectBaseConcentratorTypeList(baseConcentratorType);
        ExcelUtil<BaseConcentratorType> util = new ExcelUtil<BaseConcentratorType>(BaseConcentratorType.class);
        return util.exportExcel(list, "type");
    }
    /**
     * @Author 王琦
     * @Description //集中器型号信息导入
     * @Date 2020/6/19 19:56
     * @Param [file, updateSupport]
     * @return com.platform.common.core.domain.AjaxResult
     */
    @Log(title = "集中器类型导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("module:concentrator:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BaseConcentratorType> util = new ExcelUtil<BaseConcentratorType>(BaseConcentratorType.class);
        List<BaseConcentratorType> concentratorTypeList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = baseConcentratorTypeService.importBaseConcentratorType(concentratorTypeList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * @Author 王琦
     * @Description //导入模板
     * @Date 2020/6/19 19:56
     * @Param []
     * @return com.platform.common.core.domain.AjaxResult
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BaseConcentratorType> util = new ExcelUtil<BaseConcentratorType>(BaseConcentratorType.class);
        return util.importTemplateExcel("集中器类型信息");
    }

    /**
     * 新增集中器型号数据
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<BaseFactory> factorys = baseFactoryService.list();
        mmap.put("factorys", factorys);
        return prefix + "/add";
    }

    /**
     * 新增保存集中器型号数据
     */
    @RequiresPermissions("module:concentrator:add")
    @Log(title = "集中器型号数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BaseConcentratorType baseConcentratorType)
    {
        baseConcentratorType.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(baseConcentratorTypeService.insertBaseConcentratorType(baseConcentratorType));
    }

    /**
     * 修改集中器型号数据
     */
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
        List<BaseFactory> factorys = baseFactoryService.list();
        mmap.put("factorys", factorys);
        BaseConcentratorType baseConcentratorType = baseConcentratorTypeService.getById(typeId);
        mmap.put("baseConcentratorType", baseConcentratorType);
        return prefix + "/edit";
    }

    /**
     * 修改保存集中器型号数据
     */
    @RequiresPermissions("module:concentrator:edit")
    @Log(title = "集中器型号数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BaseConcentratorType baseConcentratorType)
    {
        baseConcentratorType.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(baseConcentratorTypeService.updateBaseConcentratorType(baseConcentratorType));
    }




    /**
     * 删除集中器型号数据
     */
    @RequiresPermissions("module:concentrator:remove")
    @Log(title = "集中器型号数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] idss = Convert.toStrArray(ids);
        for(String id : idss){
            if (baseConcentratorTypeService.selectCountConcentratorDataByTypeId(Long.parseLong(id)) > 0)
            {
                return AjaxResult.warn("存在子设备,不允许删除");
            }
        }
        return toAjax(baseConcentratorTypeService.deleteBaseConcentratorTypeByIds(ids));
    }

    /**
     * 校验集中器代码
     */
    @PostMapping("/checkConcentratorTypeCodeUnique")
    @ResponseBody
    public String checkConcentratorTypeCodeUnique(BaseConcentratorType baseConcentratorType)
    {
        return baseConcentratorTypeService.checkConcentratorTypeCodeUnique(baseConcentratorType);
    }
}
